<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试webSocket的用法</title>
</head>

<body>
    <input type="text" id="message">
    <button id="send-buttom">发送</button>

    <script>
        let webSocket = new WebSocket("ws://127.0.0.1:8080/test");//路径与后端配置注册的要匹配

        //注册回调函数

        webSocket.onopen = function () {
            console.log("连接成功");
        }

        webSocket.onclose = function () {
            console.log("连接断开");
        }
        webSocket.onerror = function () {
            console.log("连接出错");
        }

        webSocket.onmessage = function (e) {
            //收到消息自动执行

            console.log("收到消息:" + e.data);
        }

        let messageInput = document.querySelector("#message");
        let sendButton = document.querySelector("#send-buttom");
        sendButton.onclick = function () {
            console.log("发送消息");
            webSocket.send(messageInput.value);
        }
    </script>
</body>

</html>